package com.yizhuoyan.his.webapi.controller.pharmacyoperator;

import com.yizhuoyan.his.ao.DrugAo;
import com.yizhuoyan.his.dto.JSONResponse;
import com.yizhuoyan.his.entity.HosDrugsEntity;
import com.yizhuoyan.his.entity.SysConstantEntity;
import com.yizhuoyan.his.service.pharmacyoperator.DrugManageService;
import com.yizhuoyan.his.webapi.controller.pharmacyoperator.vo.DrugManageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class DrugManageController {
    @Autowired
    DrugManageService service;
    @PostMapping("/addDrug")
    public JSONResponse addDrug(DrugAo ao)throws  Exception{
        service.addDrug(ao);
        return JSONResponse.ok();
    }
    @DeleteMapping("/deleteDrug/{id}")
    public JSONResponse delete (@PathVariable Long id) throws  Exception{

        service.removeDrug(id);
        return JSONResponse.ok();
    }
    @PutMapping("/modifyDrugInfo/{id}")
    public JSONResponse modifyDrugInfo(@PathVariable Long id, DrugAo ao) throws Exception{
        service.modifyDrug(id,ao);
        return JSONResponse.ok();
    }
    @GetMapping("/queryDrugInfo/{id}")
    public JSONResponse queryDrugInfo(@PathVariable Long id)throws Exception{
        HosDrugsEntity hosDrugsEntity = service.queryDrug(id);
        return JSONResponse.ok(DrugManageVo.of(hosDrugsEntity));
    }
    @GetMapping("/queryDrugsTypeInfo")
    public JSONResponse queryDrugsTypeInfo() throws Exception{
        List<SysConstantEntity> sysConstantEntities = service.queryDrugsType();
        List<String> list = new ArrayList<>(sysConstantEntities.size());
        for(SysConstantEntity entity : sysConstantEntities){
            list.add(entity.getValue());
        }
        return JSONResponse.ok(list);
    }
    @GetMapping("/queryDrugsDosageInfo")
    public JSONResponse queryDrugsDosageInfo() throws  Exception{
        List<SysConstantEntity> sysConstantEntities = service.queryDrugsDosage();
        List<String> list = new ArrayList<>(sysConstantEntities.size());
        for (SysConstantEntity entity:sysConstantEntities){
            list.add(entity.getValue());
        }
        return JSONResponse.ok(list);
    }
}
